﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Windows.Forms;
using vkUpploaderLib;
using System.Reactive;
using System.Reactive.Concurrency;
using vkUpploaderLib.Objects;

namespace vkUpploaderApp
{
    public partial class frmSettings : Form
    {
        private List<VkGroup> _groups;

        public frmSettings()
        {
            InitializeComponent();
            this.Text = StringResources.Settings;

            this.btCreateNew.Text = StringResources.CreateNewAlbum;
            this.lbUploadAlbum.Text = StringResources.UploadAlbum;
            this.btLogout.Text = StringResources.Logout;
            this.lbLanguage.Text = StringResources.Language;
            this.lbTarget.Text = StringResources.Target;
            this.btApply.Text = StringResources.Apply;

            cbLanguage.DataSource = AvailableLanguages.Languages;
            cbLanguage.ValueMember = "Value";

            cbLanguage.Text = AvailableLanguages.GetName(Properties.Settings.Default.CurrentCulture);
            BindGroups();

            BindAlbums();
        }

        private void BindGroups()
        {
            _groups = new List<VkGroup>();
            _groups.Add(new VkGroup() { Id = null, Name = StringResources.MyPage });
            _groups.AddRange(ApiHelper.GetGroups());
            

            cbTargetGroup.Items.Clear();
            foreach (VkGroup vkGroup in _groups)
            {
                cbTargetGroup.Items.Add(vkGroup);
            }

            cbTargetGroup.SelectedIndex = 0;

            IEnumerable<VkGroup> selectedItem = _groups.Where(g => g.Id == Properties.Settings.Default.DefaultTargetId);
            var group = selectedItem.FirstOrDefault();
            if (group != null)
            {
                cbTargetGroup.SelectedItem = group;
            }
        }

        private void BindAlbums()
        {
            //if (Properties.Settings.Default.DefaultAlbum != null)
            //{
            //    cbAlbums.Text = Properties.Settings.Default.DefaultAlbum.Name;
            //}

            //if (Properties.Settings.Default.DefaultTarget != null)
            //{
            //    cbTargetGroup.Text = Properties.Settings.Default.DefaultTarget;
            //}

            if (cbTargetGroup.SelectedItem == null)
            {
                return;
            }

            List<Album> albumsList = ApiHelper.GetAlbumsForUserOrGroup(((VkGroup)cbTargetGroup.SelectedItem).Id);

            cbAlbums.DataSource = albumsList;

            string defaultAlbumId = Properties.Settings.Default.DefaultAlbumId;
            IEnumerable<Album> enumerable = albumsList.Where(album => album.Id == defaultAlbumId);
            var first = enumerable.FirstOrDefault();
            if (first != null)
            {
                cbAlbums.SelectedItem = first;
            }
        }


        private void ShowException(string message)
        {
            MessageBox.Show(message, StringResources.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }

        private void btCreateNew_Click(object sender, EventArgs e)
        {
            var id = ((VkGroup)cbTargetGroup.SelectedItem).Id;
            frmNewAlbum frmNewAlbum = new frmNewAlbum(id);
            if (frmNewAlbum.ShowDialog() == DialogResult.OK)
            {
                BindAlbums();
            }
        }

        private void btLogout_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Token = null;
            Properties.Settings.Default.TokenExpires = DateTime.MinValue;
            Properties.Settings.Default.Save();
            Close();
            Application.Restart();
        }

        private void btApply_Click(object sender, EventArgs e)
        {
            if (cbLanguage.SelectedItem != null)
            {
                Properties.Settings.Default.CurrentCulture =
                    ((KeyValuePair<string, string>) cbLanguage.SelectedItem).Key;
            }
            if (cbAlbums.SelectedItem != null)
            {
                Properties.Settings.Default.DefaultAlbum = (cbAlbums.SelectedItem as Album);
                Properties.Settings.Default.DefaultAlbumId = Properties.Settings.Default.DefaultAlbum.Id;
            }

            if (cbTargetGroup.SelectedItem != null)
            {
                Properties.Settings.Default.DefaultTarget = (cbTargetGroup.SelectedItem as VkGroup).Name;
                Properties.Settings.Default.DefaultTargetId = (cbTargetGroup.SelectedItem as VkGroup).Id;
            }

            Properties.Settings.Default.Save();
            Close();
        }

        private void cbTargetGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbTargetGroup.SelectedItem != null)
            {
                var group = (VkGroup)cbTargetGroup.SelectedItem;
                List<Album> albumsList;
                albumsList = ApiHelper.GetAlbumsForUserOrGroup(group.Id);
                cbAlbums.DataSource = albumsList;
            }
        }
    }
}
